//
//  DFAppleVC.m
//  DFMessage
//
//  Created by lanou on 15/10/26.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "DFAppleVC.h"
#import "DFAppleTableViewCell.h"
#import "Header.pch"
#import "PKRequestManager.h"
#import "DFappleModel.h"
#import "DFClickAppleViewController.h"
#import "MJRefresh.h"
@interface DFAppleVC ()

@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)NSMutableArray *DataSourceArray;
@property(nonatomic,strong)DFappleModel *model;
@property(nonatomic,assign)int page;
@end

@implementation DFAppleVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _DataSourceArray = [NSMutableArray array];
    [self.tableView registerClass:[DFAppleTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.rowHeight = 83;
    
    [self requestDataWithType:0];

    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestDataWithType:0];
    }];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self requestDataWithType:1];
    }];
}


-(void)requestDataWithType:(NSInteger)type
{
    if (type == 0) {
        _page = 1;
    }
    else
    {
        _page ++;
    }
    NSString *str = [NSString stringWithFormat:@"http://lib.wap.zol.com.cn/ipj/docList/?v=4.0&class_id=255&isReviewing=NO&last_time=2015-10-27%2010%3A49&page=%d&retina=1&vs=iph440",_page];
     [PKRequestManager requestWithType:GET urlString:str parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers |NSJSONReadingMutableLeaves error:nil];
         if (type == 0) {
             [self.DataSourceArray removeAllObjects];
         }
        NSArray *listArray = dic[@"list"];
         for (NSDictionary *modelDic in listArray) {
            
             _model = [[DFappleModel alloc]init];
             
             [_model setValuesForKeysWithDictionary:modelDic];
             [_DataSourceArray addObject:_model];
 
         }
         [self.tableView.header endRefreshing];
         [self.tableView.footer endRefreshing];
         [self.tableView reloadData];
    } error:^(NSError *error) {
        [self.tableView.footer endRefreshing];
        [self.tableView.header endRefreshing];
        
    }];
    
    
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DFappleModel *model = _DataSourceArray[indexPath.row];
    DFClickAppleViewController *appleVC = [[DFClickAppleViewController alloc]init];
    appleVC.str = model.url;
    [self.navigationController pushViewController:appleVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}

#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.
    
       return _DataSourceArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DFAppleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];
    cell.configureModel = _DataSourceArray[indexPath.row];
    
   
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
